//
//  HMStatusCell.swift
//  weibo10
//
//  Created by mac on 15/10/21.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
import SnapKit

// 设置边距常量
let statusCellMargin: CGFloat = 12
// 设置头像的宽度
let statusIconWidth: CGFloat = 35

class HMStatusCell: UITableViewCell {
    var viewModel:HMStatusViewModel?{
        didSet{
            // 设置工作
            topView.viewModel = viewModel
            // 设置微博发表内容
            txtLabel.text = viewModel?.status.text
            picView.viewModel = viewModel
            // 测试配图视图行高
            picView.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(picView.bounds.height)
                make.width.equalTo(picView.bounds.width)
            }
        }
    }
    //MARK: - 行高
    func rowHeight(vm:HMStatusViewModel) -> CGFloat {
        viewModel = vm
        // 强制更新约束
        contentView.layoutIfNeeded()
        // 返回底部视图的最大Y值
        return CGRectGetMaxY(bottomView.frame)
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        // 设置UI
        setupUI()
        // 选中样式
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 懒加载控件
    private lazy var topView: HMStatusCellTopView = HMStatusCellTopView()
    lazy var txtLabel: UILabel = UILabel(title: "发表的内容", fontSize: 15, inset: statusCellMargin)
    lazy var bottomView: HMStatusCellBottomView = HMStatusCellBottomView()
    lazy var picView: HMThumbnailView = HMThumbnailView()
}
// MARK: - 界面设置
extension HMStatusCell{
     func setupUI(){
        
        // 1. 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(txtLabel)
        contentView.addSubview(picView)
        contentView.addSubview(bottomView)
        
        // 2. 自动布局
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(statusIconWidth + 2 * statusCellMargin)
        }
        txtLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(statusCellMargin)
        }

        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(picView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
        }
    }
    
}